#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
#endregion

namespace XnaGameComponents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Stairs : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public Stairs(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        private Vector3 center;

        public Vector3 Center
        {
            get { return center; }
            set { center = value; }
        }

        private Vector3 stepDimensions;

        public Vector3 StepDimensions
        {
            get { return stepDimensions; }
            set { stepDimensions = value; }
        }
        private Vector3 firstStepPosition;

        public Vector3 FirstStepPosition
        {
            get { return firstStepPosition; }
            set { firstStepPosition = value; }
        }


        private float angle;

        public int Angle
        {
            get { return (int)Math.Round(MathHelper.ToDegrees(angle),0); }
            set { angle = MathHelper.ToRadians(value%360); }
        }
        private int stepNumber;

        public int StepNumber
        {
            get { return stepNumber; }
            set { stepNumber = value; }
        }

        private ExtrudableQuad[] steps;

        protected override void LoadContent()
        {
            base.LoadContent();
            if (Enabled && Visible)
            {
                steps = new ExtrudableQuad[stepNumber];
                if (angle == 0)
                {
                    for (int i = 0; i < stepNumber; i++)
                    {
                        Cube cube = new Cube(Game);
                        cube.Dimensions = new Vector3(stepDimensions.X, stepDimensions.Y * (i + 1), stepDimensions.Z);
                        cube.Origin = firstStepPosition + new Vector3(0, i * stepDimensions.Y / 2f, i * stepDimensions.Z);
                        steps[i] = cube.EQuad;
                    }
                }
                else
                {
                    Vector3 normal = Vector3.Left;
                    Vector3 origin = firstStepPosition + center;
                    Matrix rotation = Matrix.CreateRotationY(angle);
                    for (int i = 0; i < stepNumber; i++)
                    {
                        steps[i] = new ExtrudableQuad(Game);
                        if (angle < 0)
                        {
                            steps[i].UpperRight = origin;
                            steps[i].UpperLeft = new Vector3(origin.X + stepDimensions.X * normal.X, origin.Y, origin.Z + stepDimensions.X * normal.Z);
                            steps[i].Height = stepDimensions.Y;
                            origin = Vector3.Transform(origin, rotation) + center;
                            normal = Vector3.Transform(normal, rotation);
                            steps[i].LowerRight = origin;
                            steps[i].LowerLeft = new Vector3(origin.X + stepDimensions.X * normal.X, origin.Y, origin.Z + stepDimensions.X * normal.Z);
                            origin += new Vector3(0, stepDimensions.Y, 0);
                        }
                        else
                        {
                            steps[i].UpperLeft = origin;
                            steps[i].UpperRight = new Vector3(origin.X + stepDimensions.X * normal.X, origin.Y, origin.Z + stepDimensions.X * normal.Z);
                            steps[i].Height = stepDimensions.Y;
                            origin = Vector3.Transform(origin, rotation) + center;
                            normal = Vector3.Transform(normal, rotation);
                            steps[i].LowerLeft = origin;
                            steps[i].LowerRight = new Vector3(origin.X + stepDimensions.X * normal.X, origin.Y, origin.Z + stepDimensions.X * normal.Z);
                            origin += new Vector3(0, stepDimensions.Y, 0);
                        }
                        Game.Components.Add(steps[i]);
                    }
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (Visible && Enabled)
            {
                Camera camera = (Camera)Game.Services.GetService(typeof(Camera));
                Ray ray = new Ray(camera.Position, Vector3.Down);
                float mind = 19052007f;
                foreach (ExtrudableQuad step in steps)
                {
                    float? d = step.Intersects(ray);
                    if (d.HasValue)
                        mind = Math.Min(mind, d.Value);
                }
                if (mind != 19052007)
                    camera.Move(new Vector3(0, 170 - mind, 0), true);
            }
        }
    }
}


